/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;
import domain.DBConnection;
import domain.User;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author admin
 */
public class RegistrationPersistence {
    
    /**
     * Adds a new user to the system
     * @param user A user object that contains details about the user to be added
     * @return true if successfully added
     */
    public boolean addNewUser(User user){
        String insertSQL = "INSERT INTO users VALUES (null,'"+user.getFirstName()+"','"+user.getLastName()+"','"+user.getUsername()+"','"+user.getPassword()+"','"+user.getEmail()+"');";
        DBConnection con = DBConnection.getDatabaseConn();
        con.connect();
        boolean updateStatus = con.update(insertSQL);
        con.disconnect();
        return updateStatus;
    }

    /**
     * Checks if a given username already exists
     * @param username the username to be checked
     * @return 1 if the username already exists
     */
    public int validateUsername(String username) {
        int resultCount = 1;
        String validateSQL = "SELECT count(`username`) FROM users WHERE `username` = '"+username+"'";
        DBConnection dbCon = DBConnection.getDatabaseConn();
        dbCon.connect();
        ResultSet rs =  dbCon.fetch(validateSQL);
        try {
            rs.first();
            resultCount = rs.getInt(1);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        dbCon.disconnect();
        return resultCount;
    }
    
}
